﻿///*
// *================================模块基本信息描述============================================
// *模块名称：FlowBasedOB.cs
// *开发语言及版本 C# 4.0
// *依赖条件： 
// *影响条件：
// *功能描述： 基于流程的 业务对象基类
// *开发者: 刘向明
// *版    本：v1.0.0
// *完成日期：2012-4-21
// *===================================模块修改记录==============================================
// *修改历史： 
// * 
// * 
// *=============================================================================================
//*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace DuJianliang.ApplicationPlatform.Model
{
    public class FlowBasedOB
    {
        public string FlowID { get; set; }
        public Guid FlowInstanceID { get; set; }
        public string Operator { get; set; }
        public DateTime OperateDate { get; set; }

        public static T LoadFromSession<T>(string key)
        {
            if (string.IsNullOrEmpty(key)) return default(T);
            object obj = System.Web.HttpContext.Current.Session[key];
            return (T)obj;
        }
        public static object LoadFromSession(string key)
        {
            if (string.IsNullOrEmpty(key)) return null;
            object obj = System.Web.HttpContext.Current.Session[key];
            return obj;
        }
        public static void SetSession(string key, object obj)
        {
            if (string.IsNullOrEmpty(key)) return;
            System.Web.HttpContext.Current.Session[key] = obj;
        }
        public static void ClearFromSession(string key)
        {
            if (string.IsNullOrEmpty(key)) return;
            System.Web.HttpContext.Current.Session.Remove(key);  
        }
        public static string GetSessioinKey(Guid flowInstanceID)
        {
            return string.Format("{0}:{1}", Common.CurrentInfo.Instance .CurrentUser .UserName  , flowInstanceID);
        }
        public static string GetSessioinKey(string key)
        {
            return string.Format("{0}:{1}", Common.CurrentInfo.Instance.CurrentUser.UserName, key);
        }
    }
    public class GridPageEntity<T>
    {
        public GridPageEntity() { }
        public GridPageEntity(IList<T> rows, int total,int no,int pageSize)
        {
            this.Rows = rows;
            this.PageCount = total;
            PageNo = no;
            PageSize = pageSize;
        }
        public IList<T> Rows { get; set; }
        public int  PageCount { get; set; }
        public int  PageNo { get; set; }
        public int PageSize { get; set; }
    }

    public class DepartmentSimpleOB
    { 
        public int DeptID { get; set; }
        public string DeptName { get; set; }
        public static IList<DepartmentSimpleOB> GetDepartmentSimpleList()
        {
            List<DepartmentSimpleOB> result=new List<DepartmentSimpleOB>() ;

            string sql = @"select  DeptID,DeptName from t5715_Department ";
            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(sql); 
            DepartmentSimpleOB obj = null;
            using (IDataReader r = database.ExecuteReader(dbCommand))
            { 
                while (r.Read())
                {
                    obj = new DepartmentSimpleOB(); 
                    obj.DeptID = (int)r["DeptID"];
                    obj.DeptName = (r["DeptName"]) as string; 
                    result.Add(obj);
                }
            }

            return result;

        }
    }
    public class  DistNumberSimpleOB
    {
        public string ItemCode { get; set; }
        public string DistNumber { get; set; }
        // public string FullDisplayName { get { return string.Format("{0:10}", ItemCode + "|" + DistNumber); } }
        public static  DistNumberSimpleOB  GetDepartmentSimpleOBbyCode(string itemcode)
        {
            DistNumberSimpleOB result = null ;

            string sql = @"SELECT T0.ItemCode,DistNumber
FROM OSRQ T0
JOIN OWHS T1 ON T1.WhsCode=T0.WhsCode
JOIN OSRN T2 ON T2.SysNumber=t0.SysNumber
WHERE T1.U_Class1=103 AND U_Class2=201 And  T0.ItemCode= @ItemCode";
            Database database = DatabaseFactory.CreateDatabase( Common.Helper.Instance.ConnectionStringName_SBODB );
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@ItemCode", DbType.String , itemcode);
            using (IDataReader r = database.ExecuteReader(dbCommand))
            {
                if (r.Read())
                {
                    result = new DistNumberSimpleOB();
                    result.ItemCode = r["ItemCode"] as string;
                    result.DistNumber = r["DistNumber"] as string; 
                }
            }

            return result;

        }
    }
    public class  WhsSimpleOB
    {
        public string ItemCode { get; set; }
        public string WhsCode { get; set; }
        public string WhsName { get; set; }
       // public string FullDisplayName { get { return string.Format("{0:10}", ItemCode + "|" + DistNumber); } }
        public static IList<WhsSimpleOB> GetWshSimpleList()
        {
            List<WhsSimpleOB> result = new List<WhsSimpleOB>();

            string sql = @"SELECT T0.ItemCode,T0.WhsCode,T1.WhsName
FROM OITW T0
JOIN OWHS T1 ON T1.WhsCode=T0.WhsCode
WHERE T1.U_Class1=103 AND U_Class2=201
AND T0.Onhand>0";
            Database database = DatabaseFactory.CreateDatabase(Common.Helper.Instance.ConnectionStringName_SBODB);
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            
            using (IDataReader r = database.ExecuteReader(dbCommand))
            {
                while (r.Read())
                {
                    WhsSimpleOB obj = new WhsSimpleOB();
                    obj.ItemCode = r["ItemCode"] as string;
                    obj.WhsCode = r["WhsCode"] as string;
                    obj.WhsName = r["WhsName"] as string;
                    result.Add(obj);
                }
            }
            #if DEBUG
            result.Add(new WhsSimpleOB() { ItemCode ="aa", WhsCode ="as",  WhsName ="sdfff" }); 

            #endif
            return result;

        }
        public static GridPageEntity<WhsSimpleOB> GetWshSimpleListPaged(int pageno, int pagesize)
        {
            GridPageEntity<WhsSimpleOB> result = new GridPageEntity<WhsSimpleOB>();
            result.PageNo = pageno;
            result.PageSize = pagesize;
            List<WhsSimpleOB> list = new List<WhsSimpleOB>();

            string sql = @"SELECT T0.ItemCode,T0.WhsCode,T1.WhsName
FROM OITW T0
JOIN OWHS T1 ON T1.WhsCode=T0.WhsCode
WHERE T1.U_Class1=103 AND U_Class2=201
AND T0.Onhand>0";

            sql = string.Format(@"Select * From ( SELECT (ROW_NUMBER() OVER(ORDER BY T1.WhsCode  Asc)) AS rownum,T0.ItemCode,T0.WhsCode,T1.WhsName
FROM OITW T0
JOIN OWHS T1 ON T1.WhsCode=T0.WhsCode
WHERE T1.U_Class1=103 AND U_Class2=201
AND T0.Onhand>0 ) T where rownum BETWEEN {0} AND {1}", (pageno - 1) * pagesize + 1, pageno * pagesize);
            Database database = DatabaseFactory.CreateDatabase(Common.Helper.Instance.ConnectionStringName_SBODB);
            DbCommand dbCommand = database.GetSqlStringCommand(sql);

            using (IDataReader r = database.ExecuteReader(dbCommand))
            {
                while (r.Read())
                {
                    WhsSimpleOB obj = new WhsSimpleOB();
                    obj.ItemCode = r["ItemCode"] as string;
                    obj.WhsCode = r["WhsCode"] as string;
                    obj.WhsName = r["WhsName"] as string;
                    list.Add(obj);
                }
            }
#if DEBUG
            list.Add(new WhsSimpleOB() { ItemCode = "aa", WhsCode = "as", WhsName = "sdfff" });

#endif
            result.Rows = list;
            dbCommand = database.GetSqlStringCommand(@"SELECT COUNT(T0.ItemCode) 
FROM OITW T0
JOIN OWHS T1 ON T1.WhsCode=T0.WhsCode
WHERE T1.U_Class1=103 AND U_Class2=201
AND T0.Onhand>0");
            IDataReader reader = database.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                int rowcount = reader.GetInt32(0);
                result.PageCount = (rowcount % pagesize == 0) ? rowcount / pagesize : rowcount / pagesize + 1;
            } reader.Close();

            return result;

        }
        public static  WhsSimpleOB GetWshSimpleOBByDocEntry(int docEntry)
        {
            WhsSimpleOB obj =  null ;

            string sql = @"SELECT T0.ItemCode,T0.WhsCode,T1.WhsName
FROM POR1 T0
JOIN OWHS T1 ON T1.whscode=T0.whscode
WHERE T0.DocEntry=@DocEntry";
            Database database = DatabaseFactory.CreateDatabase(Common.Helper.Instance.ConnectionStringName_SBODB);
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@DocEntry", DbType.Int32, docEntry);
            using (IDataReader r = database.ExecuteReader(dbCommand))
            {
                if (r.Read())
                {
                    obj = new WhsSimpleOB();       
                    obj.ItemCode = r["ItemCode"] as string;
                    obj.WhsCode = r["WhsCode"] as string;
                    obj.WhsName = r["WhsName"] as string;
                    
                }
            }
#if DEBUG
            obj=(new WhsSimpleOB() { ItemCode = "aa", WhsCode = "as", WhsName = "sdfff" });

#endif
            return obj;

        }
    }
    public class CardCodeSimpleOB
    {
        public string CardCode { get; set; }
        public string CardName { get; set; }
        public static IList<CardCodeSimpleOB> GetCardCodeSimpleList()
        {
            List<CardCodeSimpleOB> result = new List<CardCodeSimpleOB>();

            string sql = @"Select CardCode  ,CardName  
FROM OCRD
WHERE CardType='S'";
            Database database = DatabaseFactory.CreateDatabase(Common.Helper.Instance.ConnectionStringName_SBODB);
            DbCommand dbCommand = database.GetSqlStringCommand(sql);

            using (IDataReader r = database.ExecuteReader(dbCommand))
            {
                while (r.Read())
                {
                    CardCodeSimpleOB obj = new CardCodeSimpleOB();
                    obj.CardCode = r["CardCode"] as string;
                    obj.CardName = r["CardName"] as string;
                   
                    result.Add(obj);
                }
            } 
            return result;
        }
        public static GridPageEntity<CardCodeSimpleOB> GetCardCodeSimplePaged(int pageno, int pagesize)
        {
            GridPageEntity<CardCodeSimpleOB> result = new GridPageEntity<CardCodeSimpleOB>();
            result.PageNo = pageno;
            result.PageSize = pagesize;
            List<CardCodeSimpleOB> list = new List<CardCodeSimpleOB>();

            string sql = @"Select CardCode  ,CardName  
FROM OCRD
WHERE CardType='S'";
            sql = string.Format(@"Select * From (Select (ROW_NUMBER() OVER(ORDER BY CardCode  Asc)) AS rownum,CardCode,CardName From OCRD WHERE CardType='S' ) t Where rownum BETWEEN {0} AND {1}", (pageno - 1) * pagesize + 1, pageno * pagesize);
 
            Database database = DatabaseFactory.CreateDatabase(Common.Helper.Instance.ConnectionStringName_SBODB);
            DbCommand dbCommand = database.GetSqlStringCommand(sql);

            using (IDataReader r = database.ExecuteReader(dbCommand))
            {
                while (r.Read())
                {
                    CardCodeSimpleOB obj = new CardCodeSimpleOB();
                    
                    obj.CardCode = r["CardCode"] as string;
                    obj.CardName = r["CardName"] as string;

                    list.Add(obj);
                }
            }
            dbCommand = database.GetSqlStringCommand("Select COUNT(CardCode) From [OCRD]");
            IDataReader    reader = database.ExecuteReader(dbCommand);
                if (reader.Read()) {
                    int rowcount = reader.GetInt32(0);
                    result.PageCount = (rowcount % pagesize == 0) ? rowcount / pagesize : rowcount / pagesize + 1;
                } reader.Close();
            result.Rows = list;
            return result;
        }
    }
    public class DocEntrySimpleOB
    {
        public string DocEntry { get; set; }
        public string DocNum { get; set; }
        public string DocType { get; set; }
        public string CardCode { get; set; }
        //public string CardName { get; set; }
        public static IList<DocEntrySimpleOB> GetDocEntrySimpleOBByCardCode(string cardcode)
        {
            IList<DocEntrySimpleOB> result = new List<DocEntrySimpleOB >();

            string sql = @"SELECT DocEntry,DocNum,DocType,CardCode
FROM OPOR
WHERE docstatus='O' AND cardcode=@cardcode";
            Database database = DatabaseFactory.CreateDatabase(Common.Helper.Instance.ConnectionStringName_SBODB);
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@cardcode", DbType.String, cardcode);
            using (IDataReader r = database.ExecuteReader(dbCommand))
            {
                while (r.Read())
                {
                    DocEntrySimpleOB obj = new DocEntrySimpleOB();
                    obj.DocEntry = r["DocEntry"] as string;
                    obj.DocNum = r["DocNum"] as string;
                    obj.DocType = r["DocType"] as string;
                    obj.CardCode = r["CardCode"] as string;
                    result.Add(obj);
                }
            }
#if DEBUG
            result.Add(new DocEntrySimpleOB() { CardCode ="wqe", DocEntry ="12", DocNum ="23", DocType ="LLD" });
#endif
            return result;
        }
        public static DocEntrySimpleOB GetDocEntrySimpleOBByDocEntry(int docEntry)
        {
            DocEntrySimpleOB obj = null;
            string sql = @"SELECT DocEntry,DocNum,DocType,CardCode
FROM OPOR
WHERE docstatus='O' AND DocEntry=@DocEntry";
            Database database = DatabaseFactory.CreateDatabase(Common.Helper.Instance.ConnectionStringName_SBODB);
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@DocEntry", DbType.Int32, docEntry);
            using (IDataReader r = database.ExecuteReader(dbCommand))
            {
                if (r.Read())
                {
                    obj = new DocEntrySimpleOB();
                    obj.DocEntry = r["DocEntry"] as string;
                    obj.DocNum = r["DocNum"] as string;
                    obj.DocType = r["DocType"] as string;
                    obj.CardCode = r["CardCode"] as string;
                   
                }
            }
#if DEBUG
           obj=new DocEntrySimpleOB() { CardCode = "wqe", DocEntry = "12", DocNum = "23", DocType = "LLD" } ;
#endif
            return obj;
        }
    
    }
}